package com.zhao.fishingguideserver.mapper;

import com.zhao.fishingguideserver.POJO.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommentMapper {

    @Insert("INSERT INTO comments(post_id, user_id, content, like_count, status) " +
            "VALUES(#{postId}, #{userId}, #{content}, #{likeCount}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(Comment comment);

    @Update("<script>" +
            "UPDATE comments " +
            "<set>" +
            "  <if test=\"content != null\">content = #{content},</if>" +
            "  <if test=\"likeCount != null\">like_count = #{likeCount},</if>" +
            "  <if test=\"status != null\">status = #{status},</if>" +
            "  update_time = now()" +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    int updateComment(Comment comment);

    @Delete("DELETE FROM comments WHERE id = #{id}")
    int deleteComment(Integer id);

    /**
     * 根据ID查询评论详情
     */
    @Select("SELECT c.id, c.post_id as postId, c.user_id as userId, c.content, c.like_count as likeCount, " +
            "c.status, c.create_time as createTime, c.update_time as updateTime, " +
            "u.username, u.nick_name as nickName " +
            "FROM comments c " +
            "LEFT JOIN users u ON c.user_id = u.user_id " +
            "WHERE c.id = #{id} AND c.status = 'published'")
    Comment selectById(@Param("id") Integer id);

    /**
     * 查询所有评论（管理员用）
     */
    @Select("SELECT c.id, c.post_id as postId, c.user_id as userId, c.content, c.like_count as likeCount, " +
            "c.status, c.create_time as createTime, c.update_time as updateTime, " +
            "u.username, u.nick_name as nickName " +
            "FROM comments c " +
            "LEFT JOIN users u ON c.user_id = u.user_id " +
            "ORDER BY c.create_time DESC")
    List<Comment> selectAll();
    
    /**
     * 统计评论数量（管理端，支持postId与关键词）
     */
    @Select("<script>" +
            "SELECT COUNT(1) FROM comments " +
            "<where>" +
            "<if test='postId != null'> AND post_id = #{postId} </if>" +
            "<if test='keyword != null and keyword != \"\"'> AND content LIKE CONCAT('%', #{keyword}, '%') </if>" +
            "</where>" +
            "</script>")
    int countComments(@Param("postId") Integer postId,
                      @Param("keyword") String keyword);
    
    /**
     * 分页查询评论（管理端，支持postId与关键词）
     */
    @Select("<script>" +
            "SELECT c.id, c.post_id as postId, c.user_id as userId, c.content, c.like_count as likeCount, " +
            "c.status, c.create_time as createTime, c.update_time as updateTime, " +
            "u.username, u.nick_name as nickName " +
            "FROM comments c " +
            "LEFT JOIN users u ON c.user_id = u.user_id " +
            "<where>" +
            "<if test='postId != null'> AND c.post_id = #{postId} </if>" +
            "<if test='keyword != null and keyword != \"\"'> AND c.content LIKE CONCAT('%', #{keyword}, '%') </if>" +
            "</where>" +
            "ORDER BY c.create_time DESC " +
            "LIMIT #{offset}, #{limit}" +
            "</script>")
    List<Comment> selectCommentsPaged(@Param("postId") Integer postId,
                                      @Param("keyword") String keyword,
                                      @Param("offset") Integer offset,
                                      @Param("limit") Integer limit);

    /**
     * 根据帖子ID查询评论列表
     */
    @Select("SELECT c.id, c.post_id as postId, c.user_id as userId, c.content, c.like_count as likeCount, " +
            "c.status, c.create_time as createTime, c.update_time as updateTime, " +
            "u.username, u.nick_name as nickName " +
            "FROM comments c " +
            "LEFT JOIN users u ON c.user_id = u.user_id " +
            "WHERE c.post_id = #{postId} AND c.status = 'published' " +
            "ORDER BY c.create_time ASC " +
            "LIMIT #{offset}, #{limit}")
    List<Comment> selectByPostId(@Param("postId") Integer postId, @Param("offset") Integer offset, @Param("limit") Integer limit);

    /**
     * 根据用户ID查询评论列表
     */
    @Select("SELECT c.id, c.post_id as postId, c.user_id as userId, c.content, c.like_count as likeCount, " +
            "c.status, c.create_time as createTime, c.update_time as updateTime, " +
            "u.username, u.nick_name as nickName " +
            "FROM comments c " +
            "LEFT JOIN users u ON c.user_id = u.user_id " +
            "WHERE c.user_id = #{userId} AND c.status = 'published' " +
            "ORDER BY c.create_time DESC " +
            "LIMIT #{offset}, #{limit}")
    List<Comment> selectByUserId(@Param("userId") Integer userId, @Param("offset") Integer offset, @Param("limit") Integer limit);

    /**
     * 更新评论点赞数
     */
    @Update("UPDATE comments SET like_count = (SELECT COUNT(*) FROM comment_likes WHERE comment_id = #{commentId}) WHERE id = #{commentId}")
    int updateLikeCount(@Param("commentId") Integer commentId);

    /**
     * 统计帖子的评论数
     */
    @Select("SELECT COUNT(*) FROM comments WHERE post_id = #{postId} AND status = 'published'")
    int countByPostId(@Param("postId") Integer postId);

    /**
     * 统计用户的评论数
     */
    @Select("SELECT COUNT(*) FROM comments WHERE user_id = #{userId} AND status = 'published'")
    int countByUserId(@Param("userId") Integer userId);
}
